<template>
  <div>
    <van-nav-bar title="注册" left-arrow @click-left="$router.back()" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '账号为必填项' },{pattern:/^[a-zA-Z0-9_]{5,8}$/,message:'长度为5到8位,只能出现数字、字母、下划线'}]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '密码为必填项' },{pattern:/^[a-zA-Z0-9_]{5,12}$/,message:'长度为5到12位,只能出现数字、字母、下划线'}]"
      />
      <div style="margin: 16px">
        <van-button block type="primary" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
    <router-link class="link" to="login">点我回首页 已有帐号,去登陆~</router-link>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'RegisterPage',
  components: {},
  props: {},
  data () {
    return {
      username: '',
      password: ''
    }
  },
  watch: {},
  computed: {},
  created () {},
  mounted () {},
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      const { data } = await register(values)
      console.log(data)
      if (data.status === 400) {
        return this.$toast(data.description)
      }
      this.$toast('注册成功')
      this.$router.push('/login')
    }
  }
}
</script>
<style lang="less" scoped>
.link{
    font-size: 12px;
    color: #5e5e5e;
    display: flex;
    justify-content: center;
    margin-top: 20px;
}
</style>
